package com.example.study.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.study.domain.*;
import com.example.study.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequiredArgsConstructor
@RequestMapping("/course")
public class CourseController {

    private final ICourseService courseService;

    private final ITeacherService teacherService;

    private final IClasszService classzService;

    private final ISelectCourseService selectCourseService;

    private final IStudentService studentService;

    /**
     * 课程列表页
     * @param session
     * @return
     */
    @GetMapping("/list")
    public String coursePage(HttpSession session) {
        List<CourseDO> listCourseDO = courseService.list();
        listCourseDO = listCourseDO.stream().map(item ->{
            item.setTeacherName(teacherService.getById(item.getTeacherId()).getName());
            item.setClasszName(classzService.getById(item.getClasszId()).getName());
            return item;
        }).collect(Collectors.toList());
        session.setAttribute("courses",listCourseDO);
        return "course_list";
    }

    /**
     * 添加课程页面
     * @param session
     * @return
     */
    @GetMapping("/add")
    public String courseAddPage(HttpSession session) {
        List<TeacherDO> teacherDOS = teacherService.list();
        session.setAttribute("teachers",teacherDOS);
        List<ClasszDO> listClasszDO = classzService.list();
        session.setAttribute("classzs",listClasszDO);
        return "course_add";
    }

    /**
     * 添加课程接口
     * @param courseDO
     * @return
     */
    @PostMapping("/add")
    public String courseAdd(CourseDO courseDO) {
        if (StrUtil.indexOf(courseDO.getStartTime(), 'T') > 0) {
            courseDO.setStartTime(courseDO.getStartTime().replace("T", " "));
        }
        this.courseService.save(courseDO);
        return "redirect:/course/list";
    }

    /**
     * 删除课程
     * @param id 课程id
     * @return
     */
    @GetMapping("/deleteData")
    @ResponseBody
    public String courseDel(Long id) {
        this.courseService.removeById(id);
        return "";
    }

    /**
     * 选课接口
     * @param id 课程id
     * @param studentId 学生id
     * @return
     */
    @GetMapping("/select")
    @ResponseBody
    public String select(Long id,Long studentId) {
        SelectCourseDO selectCourseDO = new SelectCourseDO();
        selectCourseDO.setStudentId(studentId);
        selectCourseDO.setCourseId(id);
        selectCourseService.save(selectCourseDO);
        return "";
    }

    /**
     * 选课页面
     * @param studentId 学生id
     * @param httpSession
     * @return
     */
    @GetMapping("/selectPage")
    public String selectPage(Long studentId,HttpSession httpSession) {
        StudentDO byIdStudentDO = studentService.getById(studentId);

        List<Long> selectedCourseIds = selectCourseService.list(new LambdaQueryWrapper<SelectCourseDO>()
                .eq(SelectCourseDO::getStudentId, studentId)).stream().map(item -> item.getCourseId()).collect(Collectors.toList());
        List<CourseDO> notSelectCourse = new ArrayList<>();
        if (selectedCourseIds.size() > 0) {
            notSelectCourse = courseService.list(new LambdaQueryWrapper<CourseDO>()
                    .eq(CourseDO::getClasszId, byIdStudentDO.getClassId())
                    .notIn(CourseDO::getId, selectedCourseIds));
            notSelectCourse = notSelectCourse.stream().map(item -> {
                item.setClasszName(classzService.getById(byIdStudentDO.getClassId()).getName());
                item.setTeacherName(teacherService.getById(item.getTeacherId()).getName());
                return item;
            }).collect(Collectors.toList());
        }else {
            notSelectCourse = courseService.list(new LambdaQueryWrapper<CourseDO>()
                    .eq(CourseDO::getClasszId, byIdStudentDO.getClassId()));
            notSelectCourse = notSelectCourse.stream().map(item -> {
                item.setClasszName(classzService.getById(byIdStudentDO.getClassId()).getName());
                item.setTeacherName(teacherService.getById(item.getTeacherId()).getName());
                return item;
            }).collect(Collectors.toList());
        }
        httpSession.setAttribute("notSelectCourse",notSelectCourse);

        return "select_course";
    }

    /**
     * 打分页面
     * @param session
     * @return
     */
    @GetMapping("/teacherCourseInfo")
    public String teacherCourseInfoPage(Long courseId,HttpSession session) {
        TeacherDO teacherDO = (TeacherDO) session.getAttribute("teacher");
        if (BeanUtil.isNotEmpty(teacherDO)) {
            CourseDO byIdCourseDO = courseService.getById(courseId);
            byIdCourseDO.setClasszName(classzService.getById(byIdCourseDO.getClasszId()).getName());
            byIdCourseDO.setTeacherName(teacherDO.getName());
            session.setAttribute("courseInfo", byIdCourseDO);

            List<SelectCourseDO> listSelectCourseDO = selectCourseService.list(new LambdaQueryWrapper<SelectCourseDO>()
                    .eq(SelectCourseDO::getCourseId, courseId));
            List<StudentDO> studentDOS = studentService.listByIds(listSelectCourseDO.stream().map(item -> item.getStudentId()).collect(Collectors.toList()));

            studentDOS = studentDOS.stream().map(item -> {
                item.setClasszName(classzService.getById(item.getClassId()).getName());
                SelectCourseDO oneSelectCourseDO = selectCourseService.getOne(new LambdaQueryWrapper<SelectCourseDO>()
                        .eq(SelectCourseDO::getStudentId, item.getId())
                        .eq(SelectCourseDO::getCourseId, courseId));
                item.setScore(oneSelectCourseDO.getScore());
                return item;
            }).collect(Collectors.toList());

            session.setAttribute("students",studentDOS);
        }
        return "teacher_course_list";
    }

    @GetMapping("/scoreInput")
    @ResponseBody
    public String scoreInput(Long stuId, Long courseId, BigDecimal score) {
        SelectCourseDO oneSelectCourseDO = selectCourseService.getOne(new LambdaQueryWrapper<SelectCourseDO>()
                .eq(SelectCourseDO::getCourseId, courseId)
                .eq(SelectCourseDO::getStudentId, stuId));
        if (BeanUtil.isNotEmpty(oneSelectCourseDO)) {
            oneSelectCourseDO.setScore(score);
            selectCourseService.updateById(oneSelectCourseDO);
        }
        return "";
    }
}
